package com.express.controller;


import com.express.auto.JSONAuthentication;
import com.express.entity.ExCar;
import com.express.entity.ExContact;
import com.express.mapper.ExContactMapper;
import com.express.response.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author express
 * @since 2022-01-26
 */
@RestController
@RequestMapping("/ex-contact")
public class ExContactController extends JSONAuthentication {

    @Autowired
    private ExContactMapper exContactMapper;

    @ApiOperation(value = "添加联系人")
    @PostMapping("/add")
    public void addContact(HttpServletRequest request, HttpServletResponse response, @RequestBody ExContact exContact) throws  Exception{
     int i= exContactMapper.insert(exContact);
     if(i>0){
         Result result=Result.ok();
         this.WriteJSON(request,response,result);
     }else {
         Result result=Result.error();
         this.WriteJSON(request,response,result);
     }

    }
    @ApiOperation(value = "删除联系人")
    @PostMapping("/del")
    public void delContact(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int  id)throws  Exception{
       int i= exContactMapper.deleteById(id);
       if(i>0){
           Result result=Result.ok();
           this.WriteJSON(request,response,result);
       }else{
           Result result=Result.error();
           this.WriteJSON(request,response,result);
       }

    }

    @ApiOperation(value = "通过id修改联系人信息")
    @PostMapping("/upd")
    public void updContact(HttpServletRequest request, HttpServletResponse response, @RequestBody ExContact exContact) throws Exception{
        int i=exContactMapper.updateById(exContact);

        if(i>0){
            Result result=Result.ok();
            this.WriteJSON(request,response,result);
        }else{
            Result result=Result.error();
            this.WriteJSON(request,response,result);
        }
    }

    @ApiOperation(value = "通过用户id查询联系人")
    @PostMapping("/look")
    public void lookContact(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int  userId) throws Exception{
      List<ExContact> list= exContactMapper.selectContactByuserId(userId);
        Result result=Result.ok().data("context",list);
        this.WriteJSON(request,response,result);
    }
}

